package day0301;

public class Exer01 {

    public static void main(String[] args) {
        String str = "()()(())(())()())(";

        isEffective(str);
    }

    public static boolean isEffective(String str) {
        // ()()(())(())()()
        int length = str.length();

        if (length % 2 != 0) {
            return false;
        }

        String replaceStr = str.replace("()", "");
        int newLength = replaceStr.length();

        while (length != newLength || newLength != 0) {
            length = newLength;
            replaceStr = replaceStr.replace("()", "");
            newLength = replaceStr.length();
        }

        return replaceStr.length() == 0;
    }


    public static boolean isEffective2(String str) {
        int length = str.length();

        if (length % 2 != 0) {
            return false;
        }

        int leftCount = 0;
        // 遍历
        for (int i = 0; i < length; i++) {
            if (str.charAt(i) == '(') {
                leftCount ++;
            } else {
                leftCount --;
                if (leftCount < 0){
                    return false;
                }
            }
        }
        return leftCount == 0;
    }
}
